<?php
require('lib/fpdf17/fpdf.php');

class PDF extends FPDF {

	function Header() {
		$this->Image('images/pdf_logo.png',10,7,24);
		$this->SetFont('Arial','B',13);
		$this->Cell(191,7,'Felix Martin Hi-Fi und Videostudios e.U.',0,1,R);
		$this->SetFont('Arial','',11);
		$this->Cell(191,5,'Neubaugasse 7/1/3',0,1,R);
		$this->Cell(191,5,'A-1060 Wien',0,1,R);
		$this->Line(10,29,200,29);
   	}

 	function Footer() {
 		$this->Line(10,280,200,280);

 		$this->SetXY(9,-15);
 		$this->SetFont('Arial','',8);
		$this->Cell(1,3,'Bankverbindung: Bank Austria',0,2,L);
		$this->Cell(1,3,'BLZ: 12000',0,2,L);
		$this->Cell(1,3,'KontoNr.: 098722-451-154',0,2,L);

		$this->SetXY(90,-15);
 		$this->SetFont('Arial','',8);
		$this->Cell(1,3,'FirmenbuchNr.: FN177033p',0,2,L);
		$this->Cell(1,3,'UmsatzsteuerID: ATU45782301',0,2,L);
		$this->Cell(1,3,'Gerichtsstand: Wien',0,2,L);

 		$this->SetXY(200,-15);
 		$this->SetFont('Arial','',8);
		$this->Cell(1,3,'Tel.: +43 1 330 15',0,2,R);
		$this->Cell(1,3,'e-Mail: office@martin-hifi.at',0,2,R);
		$this->Cell(1,3,'www.martin-hifi.at',0,2,R);
 	}

 	function Kunde($row) {
 		$this->SetXY(20, 49);
 		$this->SetFont('Arial','',12);

		if ((isset($row['Firma'])) || !($row['Firma'] =="")) {
			$this->Cell(1,5,'Firma',0,2,L);
			$this->Cell(1,5,$row['Firma'],0,2,L);
		} else {
			$this->Cell(1,5,$row['Anrede'],0,2,L);
			$this->Cell(1,5,$row['Vorname'] . ' ' . $row['Nachname'],0,2,L);
		}
		$this->Cell(1,5,$row['Strasse'],0,2,L);
		$this->Cell(1,5,$row['PLZ'] . ' ' . $row['Ort'],0,2,L);
		if ((isset($row['Firma'])) || !($row['Firma'] =="")) {
			$this->Cell(1,5,$row['USTIDNR'],0,2,L);
		}
 	}

 	function Barverkauf() {
 		$this->SetXY(20, 49);
 		$this->SetFont('Arial','',12);
 		$this->Cell(1,5,'Barverkauf',0,2,L);

 	}

 	function Status($row) {
 		$this->SetXY(160, 49);
 		$this->SetFont('Arial','B',11);
 		if (!isset($row[Storno])) {
 			$this->Cell(1,5,'Rechnung Nr.:',0,2,R);
 		} else {
 			$this->Cell(1,5,'Storno-Rg Nr.:',0,2,R);
		}
		$this->SetFont('Arial','',11);
 		$this->Cell(2,5,'Mitarbeiter Nr.: ',0,2,R);
 		$this->Cell(2,5,'Kunden Nr.: ',0,2,R);
 		$this->Cell(2,7,'Datum: ',0,2,R);
 		
 		$this->SetXY(190, 49);
 		$this->SetFont('Arial','B',11);
 		$this->Cell(1,5,$row['RegID'],0,2,R);
 		$this->SetFont('Arial','',11);
 		$this->Cell(1,5,$row['MitarbeiterID'],0,2,R);
 		$this->Cell(1,5,$row['KundeID'],0,2,R);
 		$this->Cell(1,7,$row['Datum'],0,2,R);
 	}

 	function Betreff($row) {
 		$this->Line(20,108,190,108);
 		$this->Line(20,117,190,117);
 		$this->Line(20,200,190,200);
 		$this->Line(20,219,69,219);
 		$this->Line(20,239,190,239);
 		
 		$this->Line(20,108,20,239);
 		$this->Line(69,200,69,239);
 		$this->Line(165,108,165,200);
 		$this->Line(190,108,190,239);

 		$this->SetXY(20,98);
 		$this->SetFont('Arial','B',16);
 		if (!isset($row[Storno])) {
 			$this->Cell(10,5,'Rechnung',0,2,L);
 		} else {
 			$this->Cell(10,5,'Storno-Rechnung',0,2,L);
 		}
 		$this->SetXY(25,110);
 		$this->SetFont('Arial','B',10);
 		$this->Cell(10,5,'Art-Nr. ',0,0,R);
 		$this->Cell(70,5,'Artikelname',0,0,L);
 		$this->Cell(21,5,'Menge',0,0,C);
 		$this->Cell(20,5,'Einzelpreis',0,0,C);
 		$this->Cell(20,5,'Mwst',0,0,C);
 		$this->Cell(24,5,'Gesamtpreis',0,0,C);
 	}

 	 function RechnungInfo($row) {
 		$this->SetFont('Arial','B',10);
 		if (!isset($row['Storno'])) {
 			$this->SetXY(141,98);
 			$this->Cell(50,6,'Leistungsdatum entspricht Rechnungsdatum',0,2,R);
 		} else {
 			$this->SetFont('Arial','',10);
 			$this->SetXY(82,98);
			$this->Cell(24,6,'von Rg-Nr.: ' . $row['StornoRg'],0,2,R);
 		}
 	}

 	function Artikel($row, $artikel, $numArtikel) {
 		$this->SetXY(25,119);
 		$this->SetFont('Arial','',10);

 		for ($i=0; $i < $numArtikel; $i++) { 
 			$this->Cell(10,5,$artikel[$i]['ArtikelID'].'  ',0,0,R);
 			$this->Cell(75,5,$artikel[$i]['Artikelname'],0,0,L);
 			$this->Cell(10,5,$artikel[$i]['Menge'],0,0,C);
 			$this->Cell(26,5,number_format($artikel[$i]['Preis'], 2, ',', '.'),0,0,R);
 			$this->Cell(21,5,$artikel[$i]['MWSt'] . '%',0,0,C);
 			$gesamt=($artikel[$i]['Preis']*$artikel[$i]['Menge']);
 			$this->Cell(22,5,number_format($gesamt, 2, ',', '.'),0,1,R);
 			$this->SetLeftMargin(25);
 			$endPreis+=$gesamt;
 			if ($artikel[$i]['MWSt']==20) {
 				$nettoPreis+=(($gesamt/120)*100);
 				$mwst20+=(($gesamt-(($gesamt/120)*100)));
 			} elseif ($artikel[$i]['MWSt']==10) {
 				$nettoPreis+=(($gesamt/110)*100);
 				$mwst10+=(($gesamt-(($gesamt/110)*100)));
 			} else {
 				$nettoPreis+=$gesamt;
 			}
 		}

 		if ($row['Rabatt']!=null || $row['Rabatt']!='0') {
 			$rabattBruttoPreis=(($endPreis*((100-$row['Rabatt'])/100)));
 			$rabattNettoPreis=(($nettoPreis*((100-$row['Rabatt'])/100)));
 			$differenz = $endPreis-$rabattBruttoPreis;
 			$mwst20=(($mwst20*((100-$row['Rabatt'])/100)));
 			$mwst10=(($mwst10*((100-$row['Rabatt'])/100)));
 		}

 		$this->SetXY(135,202);
 		$this->SetFont('Arial','B',10);
 		$this->Cell(30,5,'Gesamtsumme Brutto      EUR',0,2,R);
 		if ($row['Rabatt']==null || $row['Rabatt']=='0') {
 			$this->Cell(33,5,'',0,2,R);
 			$this->Cell(33,5,'',0,2,R);
 			$this->Cell(33,5,'',0,2,R);
 		} else {
 			$this->Cell(30,5,'- ' . $row['Rabatt'] . '% Rabatt      EUR',0,2,R);
 			$this->Cell(30,5,'Endsumme Brutto      EUR',0,2,R);
 			$this->Cell(33,5,'',0,2,R);
 		}
 		$this->Cell(30,5,'Gesamtsumme Netto      EUR',0,2,R);
 		$this->Cell(30,5,'+ 20% MWSt      EUR',0,2,R);
 		$this->Cell(30,5,'+ 10% MWSt      EUR',0,2,R);

 		$this->SetXY(156,202);
 		$this->Cell(33,5,number_format($endPreis, 2, ',', '.'),0,2,R);
 		if ($row['Rabatt']==null || $row['Rabatt']=='0') {
 			$this->Cell(33,5,'',0,2,R);
 			$this->Cell(33,5,'',0,2,R);
 			$this->Cell(33,5,'',0,2,R);
 		} else {
 			$this->Cell(33,5,number_format($differenz, 2, ',', '.'),0,2,R);
 			$this->Cell(33,5,number_format($rabattBruttoPreis, 2, ',', '.'),0,2,R);
 			$this->Cell(33,5,'',0,2,R);
 		}
 		$this->Cell(33,5,number_format(($rabattNettoPreis), 2, ',', '.'),0,2,R);
 		$this->Cell(33,5,number_format(($mwst20), 2, ',', '.'),0,2,R);
		$this->Cell(33,5,number_format(($mwst10), 2, ',', '.'),0,2,R);
 	}

	function Mitarbeiter($row) {
		$this->SetXY(22, 204);
 		$this->SetFont('Arial','',10);
 		$this->Cell(25,5,'Bearbeitet von:',0,2,L);
 		$this->Cell(120,5,$row['VN'] . ' ' . $row['NN'],0,2,L);
 	}

 	function Lieferung($row) {
 		$this->SetXY(22,222);
 		$this->SetFont('Arial','',10);
 		$this->Cell(30,5,'Lieferung:',0,2,L);
 		$this->Cell(30,5,'Lieferdatum:',0,2,L);
 		$this->Cell(30,5,'Montage:',0,2,L);

 		$this->SetXY(42,222);
 		if (isset($row['Lieferung'])) {
			$this->Cell(25,5,'ja',0,2,R);
			$this->Cell(25,5,$row['Lieferdatum'],0,2,R);
		} else {
 			$this->Cell(25,5,'nein',0,2,R);
 			$this->Cell(25,5,'-',0,2,R);
 		}
 		if ($row['Montage']!=0) {
 			$this->Cell(25,5,'ja',0,2,R);
 		} else {
 			$this->Cell(25,5,'nein',0,2,R);
 		}
 	}

 	function Info($row) {
 		$this->SetXY(20,244);
 		$this->SetFont('Arial','B',10);
 		$this->Cell(50,6,'Informationen zur Rechnung:',0,2,L);
 		$this->SetFont('Arial','',10);
 		$this->MultiCell(170,4,utf8_decode($row['Informationen']),0,L);
 	}

}

//Start
ConnectMySql();

function ConnectMySql() {
	if (isset($_POST['erstellePDF'])) {
		
   		include('inc/db_connect.php');
		
		$query = mysql_query("SELECT *, Rechnung.ID RegID, Mitarbeiter.Vorname VN, Mitarbeiter.Nachname NN, Rechnungstorno.RechnungStornoID Storno, Rechnungstorno.RechnungOriginalID StornoRg FROM Rechnung
		LEFT JOIN Lieferung on Rechnung.Lieferung=Lieferung.ID
		LEFT JOIN Mitarbeiter on Rechnung.MitarbeiterID=Mitarbeiter.ID
		LEFT JOIN Kunde on Rechnung.KundeID=Kunde.ID
		LEFT JOIN Adresse on Kunde.Adresse=Adresse.ID
		LEFT JOIN Rechnungstorno on Rechnungstorno.RechnungStornoID=Rechnung.ID 
		WHERE Rechnung.ID=" . $_POST['erstellePDF']);
		$row = mysql_fetch_array($query);	    

		$queryArtikel = mysql_query("SELECT * FROM rechnungartikel reg
			LEFT JOIN artikel on reg.ArtikelID = artikel.id
			LEFT JOIN kategorie on artikel.kategorie = kategorie.id
			WHERE reg.RechnungID=" . $_POST['erstellePDF']);
		$numArtikel=mysql_num_rows ($queryArtikel);
 		
 		for ($i=0; $i < $numArtikel; $i++) { 
           	$artikel[$i] = mysql_fetch_array($queryArtikel);
        }

		createPDF($row, $artikel, $numArtikel);
 	
 	} else {
 		
 	}
}

function CreatePDF($row, $artikel, $numArtikel) {
	$pdf = new PDF('P','mm','A4');
	$pdf->AliasNbPages();
	$pdf->AddPage();
	if (isset($row['KundeID'])) {
		$pdf->Kunde($row);
	} else {
		$pdf->Barverkauf();
	}
	$pdf->RechnungInfo($row);
	$pdf->Status($row);
	$pdf->Betreff($row);
	$pdf->Artikel($row, $artikel, $numArtikel);
	$pdf->Lieferung($row);
	$pdf->Mitarbeiter($row);
	if ((!isset($row['Informationen'])) || !($row['Informationen'] =="")) {
		$pdf->Info($row);
	}
	$pdf->Output('Rechnung_' . $row['RegID'] . '_' . $row['Datum'],I);
}
?>